package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Categoria;
import gepes.modelo.Clase;
import gepes.modelo.Profesor;
import gepes.modelo.Publicacion;
import gepes.util.ConexionBD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class CategoriaDAO extends BaseDAO{
	public Collection<Categoria> listarProfe(int id)	throws DAOExcepcion {
		String query = "select codCategoria,nombre from categoria where codcategoria in "
				+ "(select codcategoria from categoriapadre_profesor where codprofesor=?)";
		Collection<Categoria> lista = new ArrayList<Categoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Categoria pes = new 				Categoria();
				pes.setNombre(rs.getString(2));
				pes.setCodCategoria(rs.getInt(1));				
				lista.add(pes);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	public Collection<Categoria> listarlibre(int id)	throws DAOExcepcion {
		String query = "select codCategoria,nombre from categoria where codcategoria not in "
				+ "(select codcategoria from categoriapadre_profesor where codprofesor=?)";
		Collection<Categoria> lista = new ArrayList<Categoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Categoria pes = new 				Categoria();
				pes.setNombre(rs.getString(2));
				pes.setCodCategoria(rs.getInt(1));				
				lista.add(pes);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	public Collection<Categoria> listar()	throws DAOExcepcion {
		String query = "select codCategoria,nombre from categoria";
		Collection<Categoria> lista = new ArrayList<Categoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Categoria pes = new 				Categoria();
				pes.setNombre(rs.getString(2));
				pes.setCodCategoria(rs.getInt(1));				
				lista.add(pes);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	public Categoria insertar(Categoria ca) throws DAOExcepcion {
		String query = "insert into categoria(codCategoria,nombre)"
				+ "values(?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			
			stmt.setInt(1, ca.getCodCategoria());
			stmt.setString(2,ca.getNombre());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			ca.setCodCategoria(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ca;
	}
	public int existe(String cl) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select count(0) from categoria where nombre=?";

			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, cl);
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return 0;

	}
	public void insertarPadreProfesor(int idprof,int idcate) throws DAOExcepcion {
		String query = "insert into categoriapadre_profesor(codprofesor,codcategoria)"
				+ "values(?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			
			stmt.setInt(1, idprof);
			stmt.setInt(2,idcate);
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
}
